package com.feng.controller;

import com.feng.entity.RestResponse;
import com.feng.entity.vo.ArticlePageReqVo;
import com.feng.entity.vo.ArticleWriteVo;
import com.feng.entity.vo.UpdateStatusReqVo;
import com.feng.service.ArticleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/article")
public class ArticleController {

    final ArticleService articleService;

    public ArticleController(ArticleService articleService) {
        this.articleService = articleService;
    }

    //添加文章
    @PreAuthorize("@ps.hasPermission('content:article:write')")
    @PostMapping("/write")
    public RestResponse  writeArticle(@RequestBody ArticleWriteVo writeVo){
        return articleService.writeArticle(writeVo);
    }

    //前台获取文章列表
    @GetMapping("/articleList")
    public RestResponse articleList(Integer current, Integer size, Long categoryId){
        return articleService.articleList(current, size, categoryId);
    }

    @PreAuthorize("@ps.hasPermission('system:article:query')")
    //前台获取文章详情
    @GetMapping("/{id}")
    public RestResponse articleDetail(@PathVariable String id){
        return articleService.articleDetail(id);
    }

    //前台更新文章浏览量
    @PutMapping("/updateViewCount/{id}")
    public RestResponse updateViewCount(@PathVariable String id){
        return articleService.updateViewCount(id);
    }

    //前台获取文章热榜
    @GetMapping("/getHotArticle")
    public RestResponse getHotArticle(){
        return articleService.getHotArticle();
    }

    //前台获取我的文章
    @GetMapping("/myArticle")
    public RestResponse myArticle(Integer current, Integer size, Long categoryId){
        return articleService.myArticle(current, size, categoryId);
    }

    @PreAuthorize("@ps.hasPermission('system:article:query')")
    //分页获取文章
    @PostMapping("/listAllArticle")
    public RestResponse lisAllArticle(@RequestBody ArticlePageReqVo articlePage){
        return articleService.listAllArticle(articlePage);
    }

    //通过修改传来的id查询文章等信息
    @PreAuthorize("@ps.hasPermission('system:article:query')")
    @GetMapping("/getArticleById/{id}")
    public RestResponse getArticleById(@PathVariable String id){
        return articleService.getArticleById(id);
    }

    @PreAuthorize("@ps.hasPermission('system:article:edit')")
    @PutMapping("/update")
    public RestResponse updateArticle(@RequestBody ArticleWriteVo articleWriteVo){
        return articleService.updateArticle(articleWriteVo);
    }

    @PreAuthorize("@ps.hasPermission('system:article:edit')")
    @PutMapping("/updateStatus")
    public RestResponse updateStatus(@RequestBody UpdateStatusReqVo updateStatus){
        return articleService.updateStatus(updateStatus);
    }

    @PreAuthorize("@ps.hasPermission('system:article:remove')")
    @DeleteMapping("/deleteArticle/{id}")
    public RestResponse deleteArticle(@PathVariable String id){
        return articleService.deleteArticleById(id);
    }

    @PreAuthorize("@ps.hasPermission('system:article:remove')")
    @DeleteMapping("/deleteBatchArticle/{ids}")
    public RestResponse  deleteBatchArticle(@PathVariable List<String> ids){
        return articleService.deleteBatchArticle(ids);
    }

    @GetMapping("/searchArticle")
    public RestResponse searchArticle(Integer current, Integer size, String keyword){
        return articleService.searchArticle(current, size, keyword);
    }
}
